﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace teach
{
    public partial class TextPanel : PanelBase
    {
        public TextPanel()
        {
            InitializeComponent();
        }

        public Button ButtonAnswer
        {
            get
            {
                return btnNext;
            }
        }

        private float FontSize = 27.5F;
        private int count = 10;
        private bool timeouted = false;


        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
            ITextQuestion textQuestion = question as ITextQuestion;
            
            UpdateFont(textQuestion.QuestionText);
            txtQuestion.Text = textQuestion.QuestionText;
            txtAnswer.Text = "";

            //this.timeoutMeter.Interval = question.Timeout;
            this.timeoutMeter.Interval = 12000;
            this.timeoutMeter.Enabled = true;
            txtAnswer.Focus();
        }

        public override Control GetAnswerControl()
        {
            return txtAnswer;
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Visible = true;
            pctStatus.Image = image;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = true;
            if (success)
            {
                txtAnswer.BackColor = Color.LightBlue;
            }
            else
            {
                txtUserAnswer.Visible = true;
                txtAnswer.BackColor = Color.Tomato;
                txtAnswer.Text = m_currentQuestion.Answer.ToString();
                btnSuggested.Visible = true;
                btnSuggested.Refresh();
            }
        }

        private void Reset()
        {
            FontSize = 27.5F;
            timeouted = false;
            this.txtQuestion.Font = new System.Drawing.Font("Segoe UI", FontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            pctStatus.Image = null;
            pctStatus.Visible = false;
            pctStatus.Refresh();
            txtCounter.Visible = false;
            txtQuestion.Text = "";
            count = 10;
            txtAnswer.ReadOnly = false;
            txtAnswer.ForeColor = Color.Black;
            txtAnswer.BackColor = Color.White;
            txtAnswer.Text = "";
            txtUserAnswer.Visible = false;
            btnNext.Text = "Réponse";
            btnSuggested.Text = "Ma réponse devrait être acceptée";
            btnSuggested.Enabled = true;
            btnSuggested.Visible = false;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            timeoutMeter.Enabled = false;
            txtCounter.Visible = false;
            m_answered = !m_answered;
            if (m_answered)
            {
                if (txtAnswer.Text.Trim() == string.Empty && !timeouted)
                {
                    m_answered = !m_answered;
                    return;
                }
                txtUserAnswer.Text = "Vous aviez répondu:" + Environment.NewLine + Environment.NewLine + txtAnswer.Text;
                base.RaiseAnswer(txtAnswer.Text);
                btnNext.Text = "Prochain";
            }
            else
            {
                Reset();
                base.RaiseNextClicked();
            }
        }

        private void btnSuggested_Click(object sender, EventArgs e)
        {
            btnSuggested.Text = "C'est noté. Merci.";
            btnSuggested.Enabled = false;
            btnNext.Focus();
            this.ActiveControl = btnNext;

            StreamWriter writer = new StreamWriter(@"C:\Users\Christian Touzel\Dropbox\Write\ProfSuggestedAnswers.txt", true);
            writer.WriteLine("- Bonne Réponse: " + txtAnswer.Text);
            writer.WriteLine("- " + txtUserAnswer.Text);
            writer.WriteLine();
            writer.Close();
        }

        private void txtQuestion_TextChanged(object sender, EventArgs e)
        {
            //UpdateFont();
        }

        private void UpdateFont(string text)
        {
            Size size = TextRenderer.MeasureText(text, txtQuestion.Font, new Size(txtQuestion.Width, txtQuestion.Height), TextFormatFlags.WordBreak);
            if (size.Height > txtQuestion.Height)
            {
                FontSize = FontSize - 2;
                this.txtQuestion.Font = new System.Drawing.Font("Segoe UI", FontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                UpdateFont(text);
            }
        }

        private void timeoutMeter_Tick(object sender, EventArgs e)
        {
            txtCounter.Text = count.ToString();
            timeoutMeter.Interval = 2000;
            txtCounter.Visible = true;
            if (count == 0)
            {
                timeouted = true;
                btnNext.PerformClick();
            }
            count--;
        }
    }
}
